package me.zhengjie.modules.system.service.apply;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.exception.CommonUtilException;
import me.zhengjie.modules.system.domain.DisUserRight;
import me.zhengjie.modules.system.domain.WxUser;
import me.zhengjie.modules.system.enums.ApplyWorkOrderTypeEnum;
import me.zhengjie.modules.system.service.DictDetailService;
import me.zhengjie.modules.system.service.DisUserRightService;
import me.zhengjie.modules.system.service.WxUserService;
import me.zhengjie.modules.system.service.dto.DisChangeApplyDto;
import me.zhengjie.modules.system.service.dto.WxUserDto;
import me.zhengjie.modules.web.service.mybatisplus.IDisUserRightPlusService;
import me.zhengjie.modules.web.service.mybatisplus.IWxUserPlusService;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class TicketNumChangeServiceImpl implements IApplyCallbackService{

    @Autowired
    DisUserRightService disUserRightService;

    @Autowired
    private DictDetailService dictDetailService;

    @Autowired
    WxUserService wxUserService;

    @Autowired
    IWxUserPlusService wxUserPlusService;

    @Autowired
    IDisUserRightPlusService disUserRightPlusService;

    @Override
    public Integer getApplyType() {
        return ApplyWorkOrderTypeEnum.TicketNumChange.getType();
    }

    @Override
    public boolean execute(DisChangeApplyDto applyDto) {
        String json = applyDto.getApplyContent();
        if(StringUtils.isEmpty(json)){
            throw new CommonUtilException("申请内容不能为空");
        }

        DisUserRight disUserRight = JSONUtil.toBean(applyDto.getApplyContent(),DisUserRight.class);
        if(ObjectUtil.isEmpty(disUserRight.getOpenId())
                || ObjectUtil.isEmpty(disUserRight.getOpenId())
                || ObjectUtil.isEmpty(disUserRight.getRightType())){
            throw new CommonUtilException("参数校验未通过");
        }
        WxUser targetUserDTO = wxUserService.findByOpenId(disUserRight.getOpenId());
        if (Objects.isNull(targetUserDTO)){
            throw new CommonUtilException("参数传递有误");
        }
        disUserRight.setOpenId(targetUserDTO.getOpenId());
        WxUser originalUserDTO = wxUserService.findByOpenId(disUserRight.getOriginalOpenId());
        if (Objects.isNull(originalUserDTO)){
            throw new CommonUtilException("参数传递有误");
        }
        disUserRight.setOriginalOpenId(originalUserDTO.getOpenId());
        //获取赠送的课程编号
        String freeCourseNo = dictDetailService.getDictValue(ConstantUtils.IdentityItemGiveCourseDictKey);
        disUserRight.setRightStatus(1);
        disUserRight.setRightCategory(0);
        disUserRight.setRightContent(freeCourseNo);
        try {
            disUserRightService.create(disUserRight);
            //权益类型，0：线下售卖，1：线下赠送，2：赠与撤销，3：售卖撤销
            switch (disUserRight.getRightType()){
                case 0:
                case 1:
                    //源减少，目标增加
                    WxUser originalWxUser = new WxUser();
                    originalWxUser.setTicketNumber(originalUserDTO.getTicketNumber()-1);
                    LambdaQueryWrapper<WxUser> userWrapper = new LambdaQueryWrapper<>();
                    userWrapper.eq(WxUser::getOpenId,disUserRight.getOriginalOpenId());
                    wxUserPlusService.update(originalWxUser,userWrapper);

                    WxUser targetWxUser = new WxUser();
                    targetWxUser.setTicketNumber(targetUserDTO.getTicketNumber()+1);
                    LambdaQueryWrapper<WxUser> userWrapper1 = new LambdaQueryWrapper<>();
                    userWrapper1.eq(WxUser::getOpenId,disUserRight.getOpenId());
                    wxUserPlusService.update(targetWxUser,userWrapper1);
                    DisUserRight userRight = new DisUserRight();
                    userRight.setOpenId(disUserRight.getOpenId());
                    userRight.setRightCategory(0);
                    userRight.setRightType(disUserRight.getRightType());
                    userRight.setRightContent(freeCourseNo);
                    userRight.setRightStatus(ConstantUtils.Valid);
                    userRight.setOriginalOpenId(disUserRight.getOriginalOpenId());
                    disUserRightPlusService.save(userRight);
                    break;
                case 2:
                case 3:
                    //源减少，目标增加
                    WxUser originalWxUser1 = new WxUser();
                    originalWxUser1.setTicketNumber(originalUserDTO.getTicketNumber()+1);
                    LambdaQueryWrapper<WxUser> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(WxUser::getOpenId,disUserRight.getOriginalOpenId());
                    wxUserPlusService.update(originalWxUser1,wrapper);

                    WxUser targetWxUser1 = new WxUser();
                    targetWxUser1.setTicketNumber(targetUserDTO.getTicketNumber()-1);
                    LambdaQueryWrapper<WxUser> wrapper1 = new LambdaQueryWrapper<>();
                    wrapper1.eq(WxUser::getOpenId,disUserRight.getOpenId());
                    wxUserPlusService.update(targetWxUser1,wrapper1);

                    DisUserRight disUserRight1 = new DisUserRight();
                    disUserRight1.setRightStatus(ConstantUtils.Deleted);
                    LambdaQueryWrapper<DisUserRight> rightWrapper = new LambdaQueryWrapper<>();
                    rightWrapper.eq(DisUserRight::getOpenId,targetUserDTO.getOpenId());
                    rightWrapper.eq(DisUserRight::getRightType,disUserRight.getRightType());
                    rightWrapper.eq(DisUserRight::getOriginalOpenId,originalUserDTO.getOpenId());
                    disUserRightPlusService.update(disUserRight1,rightWrapper);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

}